package com.example.mvvm.activity;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.example.mvvm.BR;
import com.example.mvvm.R;
import com.example.mvvm.adapter.VideoPlayerAdapter;
import com.example.mvvm.base.BaseActivity;
import com.example.mvvm.databinding.ActivityVideoPlayerBinding;
import com.example.mvvm.mode.LookVideoMode;
import com.example.mvvm.utils.DebugUtils;
import com.example.mvvm.viewmodel.VideoPlayerModel;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener;
import com.shuyu.gsyvideoplayer.GSYVideoManager;

public class VideoPlayerActivity extends BaseActivity<VideoPlayerModel, ActivityVideoPlayerBinding> {

    @Override
    public int getLayoutId() {
        return R.layout.activity_video_player;
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {

        // 设置适配器
        getBinding().videoRv.setAdapter(getViewModel().getAdapter());
        getViewModel().getLiveData().observe(this, videoModes -> {
            getViewModel().getAdapter().setData(videoModes);
            // 播放视频
            getViewModel().playPosition(0,videoModes.get(0));
        });
        // 注册滑动监听
        getBinding().videoRv.registerOnPageChangeCallback(getViewModel().changeCallback);
        // 加载刷新
        getBinding().videoLayout.setOnRefreshLoadMoreListener(getViewModel().moreListener);
    }

    @Override
    public int variableId() {
        return BR.viewModel;
    }

    @Override
    public void onBackPressed() {
        if (GSYVideoManager.backFromWindowFull(this)) {
            return;
        }
        super.onBackPressed();
    }

    @Override
    protected void onPause() {
        super.onPause();
        GSYVideoManager.onPause();
    }

    @Override
    protected void onResume() {
        super.onResume();
        GSYVideoManager.onResume();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        GSYVideoManager.releaseAllVideos();
    }
}
